﻿/*San Antonio Health Center Patient Information Management System
 * 
 * ===================================
 * Description: addFamily Page
 * Updates:
 * -adds new family in the database
 * -connects and stores in database
 * 
 * Note: 
 * -check for valid date not yet done
 * -----------------------------------
 * Author:
 * Guerrero,Ida Louise M.
 * 
 * Date: Feb. 22, 2010
 * ====================================
 */

/**
 * ====================================
 * Updated by: Monica DM. Tosino
 * Update(s): Graphical User Interface
 * Date Finished: March 1, 2010
 * ====================================
 * **/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Text.RegularExpressions;

namespace SAHCPIMS
{
    public partial class ADDFAMILY : Form
    {
        public string homeEmpName;
        public int homeEmpNum;

        public ADDFAMILY()
        {
            InitializeComponent();
        }

        public void putAccountInfo()
        {
            empNumLabel.Text = homeEmpNum.ToString();
            employeeLabel.Text = homeEmpName;
        }

        private void Submit_Click(object sender, EventArgs e)
        {
            /*  DateTime bday = new DateTime();
                string bbirth;
                bday.Date.Equals("2/22/2009");
                bbirth = DateTime.Today.Subtract(bday).Days.ToString();
                MessageBox.Show(bbirth);x
                MessageBox.Show(DateTime.Today.ToShortDateString());
             */

            int doFlag = 0;

            //Storage of Form Values
            string fname = Firstname.Text;
            string mname = Middlename.Text;
            string lname = Lastname.Text;
            string bdate = bdaytextfield.Text;
            string address = Address.Text;

            //Match Values to Valid inputs
            Match fnameMatch = System.Text.RegularExpressions.Regex.Match(fname, @"^[A-Z][a-zA-Z]*([' 'A-Z][a-zA-Z]*)*$");
            Match mnameMatch = System.Text.RegularExpressions.Regex.Match(mname, @"^[A-Z][a-zA-Z]*([' 'A-Z][a-zA-Z]*)*$");
            Match lnameMatch = System.Text.RegularExpressions.Regex.Match(lname, @"^[A-Z][a-zA-Z]*([' 'A-Z][a-zA-Z]*)*$");
            Match addMatch = System.Text.RegularExpressions.Regex.Match(address, @"[0-9a-zA-Z]+$");
            Match bday = System.Text.RegularExpressions.Regex.Match(bdate, @"^((0[1-9])|10|11|12)/(([0-2][1-9])|10|20|30|31])/(19|20)[0-9][0-9]$");

            if (!fnameMatch.Success) MessageBox.Show("Invalid Firstname Input");
            else if (!mnameMatch.Success) MessageBox.Show("Invalid Middlename Input");
            else if (!lnameMatch.Success) MessageBox.Show("Invalid Lastname Input");
            else if (!bday.Success) MessageBox.Show("Invalid Birthdate Input");
            /** =========== Editted by: MONICA TOSINO :: Date Checking :: March 2, 2010 =========== **/
            else if ((bdate[1] == '2' && bdate.Substring(3, 2) == "29" && (int.Parse(bdate.Substring(6, 4)) % 4) != 0)
                    || ((bdate[1] == '4' || bdate[1] == '6' || bdate[1] == '9' || bdate.Substring(0,2) == "11") && bdate.Substring(3, 2) == "31"))
            {
                MessageBox.Show("Incorrect Birthdate");
            }
            /** =========================== End of Update ======================== **/
            else if (!addMatch.Success) MessageBox.Show("Invalid Address Input");
            else
            {

                string conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\SAHCPIMS_database.mdb";

                OleDbConnection conn = new OleDbConnection(conString);

                DataSet ds = new DataSet();
                try
                {

                    conn.Open();
                    OleDbDataAdapter adapter = new OleDbDataAdapter("INSERT into Family (FirstName, MiddleName, LastName, Birthday, Address) values('" + fname + "', '" + mname + "', '" + lname + "', '" + bdate + "', '" + address + "')", conn);

                    adapter.Fill(ds);
                    doFlag = 1;
                    //MessageBox.Show("Add Family Success");
                }
                catch (OleDbException)
                {
                    MessageBox.Show("Error in inserting values");
                }


                conn.Close();

                if (doFlag == 1)
                {
                    nameresult.Text = fname + " " + mname + " " + lname;
                    bdayresult.Text = bdate;
                    addressresult.Text = address;
                    conn.Open();
                    OleDbDataAdapter adapter = new OleDbDataAdapter("Select * from Family where FirstName='" + fname + "' and MiddleName='" + mname + "' and LastName='" + lname + "'", conn);
                    //OleDbDataAdapter adapter = new OleDbDataAdapter("Select max(FamilyNum) from Family", conn);
                    adapter.Fill(ds);
                    conn.Close();
                    //MessageBox.Show(ds.ToString());
                    DataTable dt = ds.Tables[0];
                    foreach (DataRow dr in dt.Rows)
                    {
                        fnumresult.Text = dr["FamilyNum"].ToString();
                    }
                    successbox.Visible = true;
                    groupBox1.Visible = false;
                }
            }
        }

        private void homebutton_Click(object sender, EventArgs e)
        {
            HOME home = new HOME();
            home.Visible = true;
            this.Visible = false;
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            Firstname.ResetText();
            Middlename.ResetText();
            Lastname.ResetText();
            bdaytextfield.ResetText();
            Address.ResetText();
        }

        private void logOutButton_Click(object sender, EventArgs e)
        {
            LOGIN logout = new LOGIN();
            logout.Visible = true;
            this.Visible = false;
        }

        private void addmemberbutton_Click(object sender, EventArgs e)
        {
            SEARCH addFamilyForm = new SEARCH();
            addFamilyForm.employeeName = homeEmpName;
            addFamilyForm.employeeNumber = homeEmpNum;
            addFamilyForm.putAccountInfo();
            addFamilyForm.addMemberFlag = 1;
            addFamilyForm.Visible = true;
            this.Visible = false;
        }

        private void addcheckupbutton_Click(object sender, EventArgs e)
        {

        }

        private void searchbutton_Click(object sender, EventArgs e)
        {
            SEARCH addFamilyForm = new SEARCH();
            addFamilyForm.employeeName = homeEmpName;
            addFamilyForm.employeeNumber = homeEmpNum;
            addFamilyForm.putAccountInfo();
            addFamilyForm.Visible = true;
            this.Visible = false;
        }

        private void addfamilybutton_Click(object sender, EventArgs e)
        {
            ADDFAMILY addFamilyForm = new ADDFAMILY();
            addFamilyForm.homeEmpName = homeEmpName;
            addFamilyForm.homeEmpNum = homeEmpNum;
            addFamilyForm.putAccountInfo();
            addFamilyForm.Visible = true;
            this.Visible = false;
        }
    }
}
